﻿using Learun.Application.Base.SystemModule;
using Learun.Util;
using Learun.Util.SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;

namespace Learun.Application.TwoDevelopment.ZZDT_EC
{
    /// <summary>
    /// 版 本 PIT-ADMS V7.0.3 敏捷开发框架
    /// Copyright (c) 2013-2018 Hexagon PPM
    /// 创 建：超级管理员
    /// 日 期：2022-04-11 09:45
    /// 描 述：项目备份表
    /// </summary>
    public class ec_project_bakupService
    {
        #region 仓储
        Repository<ec_project_bakupEntity> _projectBakupRepository => new Repository<ec_project_bakupEntity>();
        Repository<AnnexesFileEntity> _annexesFileRepository => new Repository<AnnexesFileEntity>();
        Repository<ec_enginedata_propertyEntity> _enginedataPropertyRepository => new Repository<ec_enginedata_propertyEntity>();
        Repository<ec_enginedata_pixelEntity> _enginedataPixelRepository => new Repository<ec_enginedata_pixelEntity>();
        Repository<ec_enginedataEntity> _enginedataRepository => new Repository<ec_enginedataEntity>();
        Repository<ec_objecttypelibEntity> _objecttypelibRepository => new Repository<ec_objecttypelibEntity>();
        Repository<ec_objecttypeEntity> _objecttypeRepository => new Repository<ec_objecttypeEntity>();
        Repository<ec_objecttypepEntity> _objecttypepRepository => new Repository<ec_objecttypepEntity>();
        Repository<ec_propertyEntity> _propertyRepository => new Repository<ec_propertyEntity>();
        Repository<ec_propertygEntity> _propertygRepository => new Repository<ec_propertygEntity>();
        Repository<ec_operate_logEntity> _operateLogRepository => new Repository<ec_operate_logEntity>();
        Repository<ec_drawing_publishEntity> _drawingPublishRepository => new Repository<ec_drawing_publishEntity>(); 
        Repository<ec_drawing_synEntity> _drawingSynRepository => new Repository<ec_drawing_synEntity>();
        Repository<ec_drawing_fileEntity> _drawingFileRepository => new Repository<ec_drawing_fileEntity>();
        Repository<ec_drawing_catalogueEntity> _drawingCatalogueRepository => new Repository<ec_drawing_catalogueEntity>();
        Repository<ec_library_fileEntity> _libraryFileRepository => new Repository<ec_library_fileEntity>();
        Repository<ec_library_catalogueEntity> _libraryCatalogueRepository => new Repository<ec_library_catalogueEntity>();
        Repository<ec_template_fileEntity> _templateFileRepository => new Repository<ec_template_fileEntity>();
        Repository<ec_template_catalogueEntity> _templateCatalogueRepository => new Repository<ec_template_catalogueEntity>();
        Repository<ec_dataitemEntity> _dataitemRepository => new Repository<ec_dataitemEntity>();
        Repository<ec_dataitemdetailEntity> _dataitemdetailRepository => new Repository<ec_dataitemdetailEntity>();
        Repository<ec_report_catalogueEntity> _reportCatalogueRepository => new Repository<ec_report_catalogueEntity>();
        Repository<ec_report_fileEntity> _reportFileRepository => new Repository<ec_report_fileEntity>();
        Repository<ec_enginedata_propertyhisEntity> _enginedataPropertyhisRepository => new Repository<ec_enginedata_propertyhisEntity>();
        Repository<ec_reltypeEntity> _reltypeRepository => new Repository<ec_reltypeEntity>();
        Repository<ec_enginedata_relEntity> _enginedataRelRepository => new Repository<ec_enginedata_relEntity>();
        Repository<ec_measuring_unitEntity> _measuringUnitRepository => new Repository<ec_measuring_unitEntity>();
        Repository<ec_measuring_unittypeEntity> _measuringUnittypeRepository => new Repository<ec_measuring_unittypeEntity>();
        #endregion
        #region 获取数据
        /// <summary>
        /// 获取实体数据
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public ec_project_bakupEntity GetEntity(string keyValue)
        {
            try
            {
                return _projectBakupRepository.GetById(keyValue);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }

        #endregion

        #region 提交数据

        /// <summary>
        /// 删除实体数据
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public void DeleteEntity(string keyValue)
        {
            try
            {
                _projectBakupRepository.Delete(t => t.ProjectBakupID == keyValue);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }

        /// <summary>
        /// 保存实体数据（新增、修改）
        /// <summary>
        /// <returns></returns>
        public void SaveEntity(ec_project_bakupEntity entity, AnnexesFileEntity annexesFiles)
        {
            SqlSugarHelper.Db.BeginTran();
            try
            {
                _projectBakupRepository.Insert(entity);
                _annexesFileRepository.Insert(annexesFiles);
                SqlSugarHelper.Db.CommitTran();
            }
            catch (Exception ex)
            {
                SqlSugarHelper.Db.RollbackTran();
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }

        /// <summary>
        /// 保存实体数据（新增、修改）
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public void OnlySaveEntity(string keyValue, ec_project_bakupEntity entity)
        {
            try
            {
                if (!string.IsNullOrEmpty(keyValue))
                {
                    entity.Modify(keyValue);
                    _projectBakupRepository.Update(entity);
                }
                else
                {
                    entity.Create();
                    _projectBakupRepository.Insert(entity);
                }
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }

        /// <summary>
        /// 获取sqlite里面的数据还原到数据库
        /// </summary>
        /// <param name="list_tblName">要还原的业务表名</param>
        /// <param name="sqlitePath">sqlite数据库</param>
        /// <param name="fileBackupPath">附件路劲</param>
        /// <param name="ProjectIndex">项目序号</param>
        public void RestoreBySqlite(List<string> list_tblName, string sqlitePath, string fileBackupPath, string ProjectIndex)
        {
            SqlSugarHelper.Db.BeginTran();
            try
            {
                UserInfo userInfo = LoginUserInfo.Get();
                //连接sqlite数据库
                SqliteHelper.GetConn($@"Data Source={sqlitePath}");
                DataTable dtAnnexesFile = SqliteHelper.GetData($" SELECT * FROM AnnexesFile ");
                List<AnnexesFileEntity> listAnnexesFile = Util.DataTableExtensions.ToList<AnnexesFileEntity>(dtAnnexesFile);//sqlite里面的附件数据
                List<AnnexesFileEntity> listFileByFolder = new List<AnnexesFileEntity>();//每个业务表对应的附件数据
                string newFolderId = "";//业务表重新生成的附件夹ID
                List<AnnexesFileEntity> addAnnexesFile = new List<AnnexesFileEntity>();//所有要往数据库新增的附件
                foreach (var table in list_tblName)
                {
                    if (table == "ec_enginedata_property")
                    {
                        //删除原来的表数据
                        SqlSugarHelper.Db.Ado.ExecuteCommand($" DELETE FROM ec_enginedata_property_{ProjectIndex}; ");
                        //获取sqlite数据库里面的数据
                        DataTable dt = SqliteHelper.GetData($" SELECT * FROM ec_enginedata_property ");
                        List<ec_enginedata_propertyEntity> listData = Util.DataTableExtensions.ToList<ec_enginedata_propertyEntity>(dt);
                        foreach (var da in listData)
                        {
                            //重新设置创建人创建时间，清空更新人更新时间
                            da.CreateUserID = userInfo.userId;
                            da.CreateTime = Time.MySqlTime;
                            da.UpdateTime = null;
                            da.UpdateUserID = null;
                        }
                        //db.InsertByNoMap(listData, $"ec_enginedata_property_{ ProjectIndex}");
                        _enginedataPropertyRepository.AsInsertable(listData).AS($"ec_enginedata_property_{ProjectIndex}").ExecuteCommand();
                        continue;
                    }
                    if (table == "ec_enginedata_pixel")
                    {
                        //删除原来的表数据
                        SqlSugarHelper.Db.Ado.ExecuteCommand($" DELETE FROM ec_enginedata_pixel_{ProjectIndex}; ");
                        //获取sqlite数据库里面的数据
                        DataTable dt = SqliteHelper.GetData($" SELECT * FROM ec_enginedata_pixel ");
                        List<ec_enginedata_pixelEntity> listData = Util.DataTableExtensions.ToList<ec_enginedata_pixelEntity>(dt);
                        foreach (var da in listData)
                        {
                            //重新设置创建人创建时间，清空更新人更新时间
                            da.CreateUserID = userInfo.userId;
                            da.CreateTime = Time.MySqlTime;
                            da.UpdateTime = null;
                            da.UpdateUserID = null;
                        }
                        //db.InsertByNoMap(listData, $"ec_enginedata_pixel_{ ProjectIndex}");
                        _enginedataPixelRepository.AsInsertable(listData).AS($"ec_enginedata_pixel_{ProjectIndex}").ExecuteCommand();
                        continue;
                    }
                    if (table == "ec_enginedata")
                    {
                        //删除原来的表数据
                        SqlSugarHelper.Db.Ado.ExecuteCommand($" DELETE FROM ec_enginedata_{ProjectIndex}; ");
                        //获取sqlite数据库里面的数据
                        DataTable dt = SqliteHelper.GetData($" SELECT * FROM ec_enginedata ");
                        List<ec_enginedataEntity> listData = Util.DataTableExtensions.ToList<ec_enginedataEntity>(dt);
                        foreach (var da in listData)
                        {
                            //重新设置创建人创建时间，清空更新人更新时间
                            da.CreateUserID = userInfo.userId;
                            da.CreateTime = Time.MySqlTime;
                            da.UpdateTime = null;
                            da.UpdateUserID = null;
                        }
                        //db.InsertByNoMap(listData, $"ec_enginedata_{ ProjectIndex}");
                        _enginedataRepository.AsInsertable(listData).AS($"ec_enginedata_{ProjectIndex}").ExecuteCommand();
                        continue;
                    }
                    if (table == "ec_objecttypelib")
                    {
                        //删除原来的表数据
                        SqlSugarHelper.Db.Ado.ExecuteCommand($" DELETE FROM ec_objecttypelib_{ProjectIndex}; ");
                        //获取sqlite数据库里面的数据
                        DataTable dt = SqliteHelper.GetData($" SELECT * FROM ec_objecttypelib ");
                        List<ec_objecttypelibEntity> listData = Util.DataTableExtensions.ToList<ec_objecttypelibEntity>(dt);
                        //db.InsertByNoMap(listData, $"ec_objecttypelib_{ ProjectIndex}");
                        _objecttypelibRepository.AsUpdateable(listData).AS($"ec_objecttypelib_{ProjectIndex}").ExecuteCommand();
                        continue;
                    }
                    if (table == "ec_objecttype")
                    {
                        //删除原来的表数据
                        SqlSugarHelper.Db.Ado.ExecuteCommand($" DELETE FROM ec_objecttype_{ProjectIndex}; ");
                        //获取sqlite数据库里面的数据
                        DataTable dt = SqliteHelper.GetData($" SELECT * FROM ec_objecttype ");
                        List<ec_objecttypeEntity> listData = Util.DataTableExtensions.ToList<ec_objecttypeEntity>(dt);
                        foreach (var da in listData)
                        {
                            //重新设置创建人创建时间，清空更新人更新时间
                            da.CreateUserID = userInfo.userId;
                            da.CreateTime = Time.MySqlTime;
                            da.UpdateTime = null;
                            da.UpdateUserID = null;
                        }
                        //db.InsertByNoMap(listData, $"ec_objecttype_{ ProjectIndex}");
                        _objecttypeRepository.AsInsertable(listData).AS($"ec_objecttype_{ProjectIndex}").ExecuteCommand();
                        continue;
                    }
                    if (table == "ec_objecttypep")
                    {
                        //删除原来的表数据
                        SqlSugarHelper.Db.Ado.ExecuteCommand($" DELETE FROM ec_objecttypep_{ProjectIndex}; ");
                        //获取sqlite数据库里面的数据
                        DataTable dt = SqliteHelper.GetData($" SELECT * FROM ec_objecttypep ");
                        List<ec_objecttypepEntity> listData = Util.DataTableExtensions.ToList<ec_objecttypepEntity>(dt);
                        //db.InsertByNoMap(listData, $"ec_objecttypep_{ ProjectIndex}");
                        _objecttypepRepository.AsInsertable(listData).AS($"ec_objecttypep_{ProjectIndex}").ExecuteCommand();
                        continue;
                    }
                    if (table == "ec_property")
                    {
                        //删除原来的表数据
                        SqlSugarHelper.Db.Ado.ExecuteCommand($" DELETE FROM ec_property_{ProjectIndex}; ");
                        //获取sqlite数据库里面的数据
                        DataTable dt = SqliteHelper.GetData($" SELECT * FROM ec_property ");
                        List<ec_propertyEntity> listData = Util.DataTableExtensions.ToList<ec_propertyEntity>(dt);
                        foreach (var da in listData)
                        {
                            //重新设置创建人创建时间，清空更新人更新时间
                            da.CreateUserID = userInfo.userId;
                            da.CreateTime = Time.MySqlTime;
                            da.UpdateTime = null;
                            da.UpdateUserID = null;
                        }
                        //db.InsertByNoMap(listData, $"ec_property_{ ProjectIndex}");
                        _propertyRepository.AsInsertable(listData).AS($"ec_property_{ProjectIndex}").ExecuteCommand();
                        continue;
                    }
                    if (table == "ec_propertyg")
                    {
                        //删除原来的表数据
                        SqlSugarHelper.Db.Ado.ExecuteCommand($" DELETE FROM ec_propertyg_{ProjectIndex}; ");
                        //获取sqlite数据库里面的数据
                        DataTable dt = SqliteHelper.GetData($" SELECT * FROM ec_propertyg ");
                        List<ec_propertygEntity> listData = Util.DataTableExtensions.ToList<ec_propertygEntity>(dt);
                        foreach (var da in listData)
                        {
                            //重新设置创建人创建时间，清空更新人更新时间
                            da.CreateUserID = userInfo.userId;
                            da.CreateTime = Time.MySqlTime;
                            da.UpdateTime = null;
                            da.UpdateUserID = null;
                        }
                        //db.InsertByNoMap(listData, $"ec_propertyg_{ ProjectIndex}");
                        _propertygRepository.AsInsertable(listData).AS($"ec_propertyg_{ProjectIndex}").ExecuteCommand();
                        continue;
                    }
                    if (table == "ec_operate_log")
                    {
                        //删除原来的表数据
                        SqlSugarHelper.Db.Ado.ExecuteCommand($" DELETE FROM ec_operate_log_{ProjectIndex}; ");
                        //获取sqlite数据库里面的数据
                        DataTable dt = SqliteHelper.GetData($" SELECT * FROM ec_operate_log ");
                        List<ec_operate_logEntity> listData = Util.DataTableExtensions.ToList<ec_operate_logEntity>(dt);
                        foreach (var da in listData)
                        {
                            //重新设置创建人创建时间，清空更新人更新时间
                            da.CreateUserID = userInfo.userId;
                            da.CreateTime = Time.MySqlTime;
                            da.UpdateTime = null;
                            da.UpdateUserID = null;
                        }
                        //db.InsertByNoMap(listData, $"ec_operate_log_{ ProjectIndex}");
                        _operateLogRepository.AsInsertable(listData).AS($"ec_operate_log_{ProjectIndex}").ExecuteCommand();
                        continue;
                    }
                    if (table == "ec_drawing_publish")
                    {
                        //删除原来的表数据
                        SqlSugarHelper.Db.Ado.ExecuteCommand($" DELETE FROM ec_drawing_publish_{ProjectIndex}; ");
                        //获取sqlite数据库里面的数据
                        DataTable dt = SqliteHelper.GetData($" SELECT * FROM ec_drawing_publish ");
                        List<ec_drawing_publishEntity> listData = Util.DataTableExtensions.ToList<ec_drawing_publishEntity>(dt);
                        foreach (var da in listData)
                        {
                            //重新设置创建人创建时间，清空更新人更新时间
                            da.CreateUserID = userInfo.userId;
                            da.CreateTime = Time.MySqlTime;
                            da.UpdateTime = null;
                            da.UpdateUserID = null;
                        }
                        //db.InsertByNoMap(listData, $"ec_drawing_publish_{ ProjectIndex}");
                        _drawingPublishRepository.AsInsertable(listData).AS($"ec_drawing_publish_{ProjectIndex}").ExecuteCommand();
                        continue;
                    }
                    if (table == "ec_drawing_syn")
                    {
                        //删除原来的表数据
                        SqlSugarHelper.Db.Ado.ExecuteCommand($" DELETE FROM ec_drawing_syn_{ProjectIndex}; ");
                        //获取sqlite数据库里面的数据
                        DataTable dt = SqliteHelper.GetData($" SELECT * FROM ec_drawing_syn ");
                        List<ec_drawing_synEntity> listData = Util.DataTableExtensions.ToList<ec_drawing_synEntity>(dt);
                        foreach (var da in listData)
                        {
                            //重新设置创建人创建时间，清空更新人更新时间
                            da.CreateUserID = userInfo.userId;
                            da.CreateTime = Time.MySqlTime;
                            da.UpdateTime = null;
                            da.UpdateUserID = null;
                        }
                        //db.InsertByNoMap(listData, $"ec_drawing_syn_{ ProjectIndex}");
                        _drawingSynRepository.AsInsertable(listData).AS($"ec_drawing_syn_{ProjectIndex}").ExecuteCommand();
                        continue;
                    }
                    if (table == "ec_drawing_file")
                    {
                        //删除原来的表数据
                        SqlSugarHelper.Db.Ado.ExecuteCommand($" DELETE FROM ec_drawing_file_{ProjectIndex}; ");
                        //获取sqlite数据库里面的数据
                        DataTable dt = SqliteHelper.GetData($" SELECT * FROM ec_drawing_file ");
                        List<ec_drawing_fileEntity> listData = Util.DataTableExtensions.ToList<ec_drawing_fileEntity>(dt);
                        foreach (var da in listData)
                        {
                            //重新生成F_FolderId和F_Id;
                            newFolderId = Guid.NewGuid().ToString();
                            listFileByFolder = listAnnexesFile.FindAll(x => x.F_FolderId == da.FolderId);
                            if (listFileByFolder != null && listFileByFolder.Count > 0)
                            {
                                listFileByFolder = listAnnexesFile.FindAll(x => x.F_FolderId == da.FolderId);
                                if (listFileByFolder != null && listFileByFolder.Count > 0)
                                {
                                    foreach (var annexesFile in listFileByFolder)
                                    {
                                        annexesFile.F_FilePath = Path.Combine(fileBackupPath, annexesFile.F_Id + annexesFile.F_FileExtensions);
                                        annexesFile.F_Id = Guid.NewGuid().ToString();
                                        annexesFile.F_FolderId = newFolderId;
                                    }
                                    addAnnexesFile.AddRange(listFileByFolder);
                                }
                            }
                            //重新设置FolderId和创建人创建时间，清空更新人更新时间
                            da.FolderId = newFolderId;
                            da.CreateUserID = userInfo.userId;
                            da.CreateTime = Time.MySqlTime;
                            da.UpdateTime = null;
                            da.UpdateUserID = null;
                        }
                        //db.InsertByNoMap(listData, $"ec_drawing_file_{ ProjectIndex}");
                        _drawingFileRepository.AsInsertable(listData).AS($"ec_drawing_file_{ProjectIndex}").ExecuteCommand();
                        continue;
                    }
                    if (table == "ec_drawing_catalogue")
                    {
                        //删除原来的表数据
                        SqlSugarHelper.Db.Ado.ExecuteCommand($" DELETE FROM ec_drawing_catalogue_{ProjectIndex}; ");
                        //获取sqlite数据库里面的数据
                        DataTable dt = SqliteHelper.GetData($" SELECT * FROM ec_drawing_catalogue ");
                        List<ec_drawing_catalogueEntity> listData = Util.DataTableExtensions.ToList<ec_drawing_catalogueEntity>(dt);
                        foreach (var da in listData)
                        {
                            //重新设置创建人创建时间，清空更新人更新时间
                            da.CreateUserID = userInfo.userId;
                            da.CreateTime = Time.MySqlTime;
                            da.UpdateTime = null;
                            da.UpdateUserID = null;
                        }
                        //db.InsertByNoMap(listData, $"ec_drawing_catalogue_{ ProjectIndex}");
                        _drawingCatalogueRepository.AsInsertable(listData).AS($"ec_drawing_catalogue_{ProjectIndex}").ExecuteCommand();
                        continue;
                    }
                    if (table == "ec_library_file")
                    {
                        //删除原来的表数据
                        SqlSugarHelper.Db.Ado.ExecuteCommand($" DELETE FROM ec_library_file_{ProjectIndex}; ");
                        //获取sqlite数据库里面的数据
                        DataTable dt = SqliteHelper.GetData($" SELECT * FROM ec_library_file ");
                        List<ec_library_fileEntity> listData = Util.DataTableExtensions.ToList<ec_library_fileEntity>(dt);
                        foreach (var da in listData)
                        {
                            //重新生成F_FolderId和F_Id;
                            newFolderId = Guid.NewGuid().ToString();
                            listFileByFolder = listAnnexesFile.FindAll(x => x.F_FolderId == da.FolderId);
                            if (listFileByFolder != null && listFileByFolder.Count > 0)
                            {
                                listFileByFolder = listAnnexesFile.FindAll(x => x.F_FolderId == da.FolderId);
                                if (listFileByFolder != null && listFileByFolder.Count > 0)
                                {
                                    foreach (var annexesFile in listFileByFolder)
                                    {
                                        annexesFile.F_FilePath = Path.Combine(fileBackupPath, annexesFile.F_Id + annexesFile.F_FileExtensions);
                                        annexesFile.F_Id = Guid.NewGuid().ToString();
                                        annexesFile.F_FolderId = newFolderId;
                                    }
                                    addAnnexesFile.AddRange(listFileByFolder);
                                }
                            }
                            //重新设置FolderId和创建人创建时间，清空更新人更新时间
                            da.FolderId = newFolderId;
                            da.CreateUserID = userInfo.userId;
                            da.CreateTime = Time.MySqlTime;
                            da.UpdateTime = null;
                            da.UpdateUserID = null;
                        }
                        //db.InsertByNoMap(listData, $"ec_library_file_{ ProjectIndex}");
                        _libraryFileRepository.AsInsertable(listData).AS($"ec_library_file_{ProjectIndex}").ExecuteCommand();
                        continue;
                    }
                    if (table == "ec_library_catalogue")
                    {
                        //删除原来的表数据
                        SqlSugarHelper.Db.Ado.ExecuteCommand($" DELETE FROM ec_library_catalogue_{ProjectIndex}; ");
                        //获取sqlite数据库里面的数据
                        DataTable dt = SqliteHelper.GetData($" SELECT * FROM ec_library_catalogue ");
                        List<ec_library_catalogueEntity> listData = Util.DataTableExtensions.ToList<ec_library_catalogueEntity>(dt);
                        foreach (var da in listData)
                        {
                            //重新设置创建人创建时间，清空更新人更新时间
                            da.CreateUserID = userInfo.userId;
                            da.CreateTime = Time.MySqlTime;
                            da.UpdateTime = null;
                            da.UpdateUserID = null;
                        }
                        //db.InsertByNoMap(listData, $"ec_library_catalogue_{ ProjectIndex}");
                        _libraryCatalogueRepository.AsInsertable(listData).AS($"ec_library_catalogue_{ProjectIndex}").ExecuteCommand();
                        continue;
                    }
                    if (table == "ec_template_file")
                    {
                        //删除原来的表数据
                        SqlSugarHelper.Db.Ado.ExecuteCommand($" DELETE FROM ec_template_file_{ProjectIndex}; ");
                        //获取sqlite数据库里面的数据
                        DataTable dt = SqliteHelper.GetData($" SELECT * FROM ec_template_file ");
                        List<ec_template_fileEntity> listData = Util.DataTableExtensions.ToList<ec_template_fileEntity>(dt);
                        foreach (var da in listData)
                        {
                            //重新生成F_FolderId和F_Id;
                            newFolderId = Guid.NewGuid().ToString();
                            listFileByFolder = listAnnexesFile.FindAll(x => x.F_FolderId == da.FolderId);
                            if (listFileByFolder != null && listFileByFolder.Count > 0)
                            {
                                listFileByFolder = listAnnexesFile.FindAll(x => x.F_FolderId == da.FolderId);
                                if (listFileByFolder != null && listFileByFolder.Count > 0)
                                {
                                    foreach (var annexesFile in listFileByFolder)
                                    {
                                        annexesFile.F_FilePath = Path.Combine(fileBackupPath, annexesFile.F_Id + annexesFile.F_FileExtensions);
                                        annexesFile.F_Id = Guid.NewGuid().ToString();
                                        annexesFile.F_FolderId = newFolderId;
                                    }
                                    addAnnexesFile.AddRange(listFileByFolder);
                                }
                            }
                            //重新设置FolderId和创建人创建时间，清空更新人更新时间
                            da.FolderId = newFolderId;
                            da.CreateUserID = userInfo.userId;
                            da.CreateTime = Time.MySqlTime;
                            da.UpdateTime = null;
                            da.UpdateUserID = null;
                        }
                        //db.InsertByNoMap(listData, $"ec_template_file_{ ProjectIndex}");
                        _templateFileRepository.AsInsertable(listData).AS($"ec_template_file_{ProjectIndex}").ExecuteCommand();
                        continue;
                    }
                    if (table == "ec_template_catalogue")
                    {
                        //删除原来的表数据
                        SqlSugarHelper.Db.Ado.ExecuteCommand($" DELETE FROM ec_template_catalogue_{ProjectIndex}; ");
                        //获取sqlite数据库里面的数据
                        DataTable dt = SqliteHelper.GetData($" SELECT * FROM ec_template_catalogue ");
                        List<ec_template_catalogueEntity> listData = Util.DataTableExtensions.ToList<ec_template_catalogueEntity>(dt);
                        foreach (var da in listData)
                        {
                            //重新设置创建人创建时间，清空更新人更新时间
                            da.CreateUserID = userInfo.userId;
                            da.CreateTime = Time.MySqlTime;
                            da.UpdateTime = null;
                            da.UpdateUserID = null;
                        }
                        //db.InsertByNoMap(listData, $"ec_template_catalogue_{ ProjectIndex}");
                        _templateCatalogueRepository.AsInsertable(listData).AS($"ec_template_catalogue_{ProjectIndex}").ExecuteCommand();
                        continue;
                    }
                    if (table == "ec_dataitem")
                    {
                        //删除原来的表数据
                        SqlSugarHelper.Db.Ado.ExecuteCommand($" DELETE FROM ec_dataitem_{ProjectIndex}; ");
                        //获取sqlite数据库里面的数据
                        DataTable dt = SqliteHelper.GetData($" SELECT * FROM ec_dataitem ");
                        List<ec_dataitemEntity> listData = Util.DataTableExtensions.ToList<ec_dataitemEntity>(dt);
                        foreach (var da in listData)
                        {
                            //重新设置创建人创建时间，清空更新人更新时间
                            da.CreateUserID = userInfo.userId;
                            da.CreateTime = Time.MySqlTime;
                            da.UpdateTime = null;
                            da.UpdateUserID = null;
                        }
                        //db.InsertByNoMap(listData, $"ec_dataitem_{ ProjectIndex}");
                        _dataitemRepository.AsInsertable(listData).AS($"ec_dataitem_{ProjectIndex}").ExecuteCommand();
                        continue;
                    }
                    if (table == "ec_dataitemdetail")
                    {
                        //删除原来的表数据
                        SqlSugarHelper.Db.Ado.ExecuteCommand($" DELETE FROM ec_dataitemdetail_{ProjectIndex}; ");
                        //获取sqlite数据库里面的数据
                        DataTable dt = SqliteHelper.GetData($" SELECT * FROM ec_dataitemdetail ");
                        List<ec_dataitemdetailEntity> listData = Util.DataTableExtensions.ToList<ec_dataitemdetailEntity>(dt);
                        foreach (var da in listData)
                        {
                            //重新设置创建人创建时间，清空更新人更新时间
                            da.CreateUserID = userInfo.userId;
                            da.CreateTime = Time.MySqlTime;
                            da.UpdateTime = null;
                            da.UpdateUserID = null;
                        }
                        //db.InsertByNoMap(listData, $"ec_dataitemdetail_{ ProjectIndex}");
                        _dataitemdetailRepository.AsInsertable(listData).AS($"ec_dataitemdetail_{ProjectIndex}").ExecuteCommand();
                        continue;
                    }
                    if (table == "ec_report_catalogue")
                    {
                        //删除原来的表数据
                        SqlSugarHelper.Db.Ado.ExecuteCommand($" DELETE FROM ec_report_catalogue_{ProjectIndex}; ");
                        //获取sqlite数据库里面的数据
                        DataTable dt = SqliteHelper.GetData($" SELECT * FROM ec_report_catalogue ");
                        List<ec_report_catalogueEntity> listData = Util.DataTableExtensions.ToList<ec_report_catalogueEntity>(dt);
                        foreach (var da in listData)
                        {
                            //重新设置创建人创建时间，清空更新人更新时间
                            da.CreateUserID = userInfo.userId;
                            da.CreateTime = Time.MySqlTime;
                            da.UpdateTime = null;
                            da.UpdateUserID = null;
                        }
                        //db.InsertByNoMap(listData, $"ec_report_catalogue_{ ProjectIndex}");
                        _reportCatalogueRepository.AsInsertable(listData).AS($"ec_report_catalogue_{ProjectIndex}").ExecuteCommand();
                        continue;
                    }
                    if (table == "ec_report_file")
                    {
                        //删除原来的表数据
                        SqlSugarHelper.Db.Ado.ExecuteCommand($" DELETE FROM ec_report_file_{ProjectIndex}; ");
                        //获取sqlite数据库里面的数据
                        DataTable dt = SqliteHelper.GetData($" SELECT * FROM ec_report_file ");
                        List<ec_report_fileEntity> listData = Util.DataTableExtensions.ToList<ec_report_fileEntity>(dt);
                        foreach (var da in listData)
                        {
                            //重新生成F_FolderId和F_Id;
                            newFolderId = Guid.NewGuid().ToString();
                            listFileByFolder = listAnnexesFile.FindAll(x => x.F_FolderId == da.FolderId);
                            if (listFileByFolder != null && listFileByFolder.Count > 0)
                            {
                                listFileByFolder = listAnnexesFile.FindAll(x => x.F_FolderId == da.FolderId);
                                if (listFileByFolder != null && listFileByFolder.Count > 0)
                                {
                                    foreach (var annexesFile in listFileByFolder)
                                    {
                                        annexesFile.F_FilePath = Path.Combine(fileBackupPath, annexesFile.F_Id + annexesFile.F_FileExtensions);
                                        annexesFile.F_Id = Guid.NewGuid().ToString();
                                        annexesFile.F_FolderId = newFolderId;
                                    }
                                    addAnnexesFile.AddRange(listFileByFolder);
                                }
                            }
                            //重新设置FolderId和创建人创建时间，清空更新人更新时间
                            da.FolderId = newFolderId;
                            da.CreateUserID = userInfo.userId;
                            da.CreateTime = Time.MySqlTime;
                            da.UpdateTime = null;
                            da.UpdateUserID = null;
                        }
                        //db.InsertByNoMap(listData, $"ec_report_file_{ ProjectIndex}");
                        _reportFileRepository.AsInsertable(listData).AS($"ec_report_file_{ProjectIndex}").ExecuteCommand();
                        continue;
                    }
                    if (table == "ec_enginedata_propertyhis")
                    {
                        //删除原来的表数据
                        SqlSugarHelper.Db.Ado.ExecuteCommand($" DELETE FROM ec_enginedata_propertyhis_{ProjectIndex}; ");
                        //获取sqlite数据库里面的数据
                        DataTable dt = SqliteHelper.GetData($" SELECT * FROM ec_enginedata_propertyhis ");
                        List<ec_enginedata_propertyhisEntity> listData = Util.DataTableExtensions.ToList<ec_enginedata_propertyhisEntity>(dt);
                        foreach (var da in listData)
                        {
                            //重新设置创建人创建时间，清空更新人更新时间
                            da.CreateUserID = userInfo.userId;
                            da.CreateTime = Time.MySqlTime;
                            da.UpdateTime = null;
                            da.UpdateUserID = null;
                        }
                        //db.InsertByNoMap(listData, $"ec_enginedata_propertyhis_{ ProjectIndex}");
                        _enginedataPropertyhisRepository.AsInsertable(listData).AS($"ec_enginedata_propertyhis_{ProjectIndex}").ExecuteCommand();
                        continue;
                    }
                    if (table == "ec_reltype")
                    {
                        //删除原来的表数据
                        SqlSugarHelper.Db.Ado.ExecuteCommand($" DELETE FROM ec_reltype_{ProjectIndex}; ");
                        //获取sqlite数据库里面的数据
                        DataTable dt = SqliteHelper.GetData($" SELECT * FROM ec_reltype ");
                        List<ec_reltypeEntity> listData = Util.DataTableExtensions.ToList<ec_reltypeEntity>(dt);
                        foreach (var da in listData)
                        {
                            //重新设置创建人创建时间，清空更新人更新时间
                            da.CreateUserID = userInfo.userId;
                            da.CreateTime = Time.MySqlTime;
                            da.UpdateTime = null;
                            da.UpdateUserID = null;
                        }
                        //db.InsertByNoMap(listData, $"ec_reltype_{ ProjectIndex}");
                        _reltypeRepository.AsInsertable(listData).AS($"ec_reltype_{ProjectIndex}").ExecuteCommand();
                        continue;
                    }
                    if (table == "ec_enginedata_rel")
                    {
                        //删除原来的表数据
                        SqlSugarHelper.Db.Ado.ExecuteCommand($" DELETE FROM ec_enginedata_rel_{ProjectIndex}; ");
                        //获取sqlite数据库里面的数据
                        DataTable dt = SqliteHelper.GetData($" SELECT * FROM ec_enginedata_rel ");
                        List<ec_enginedata_relEntity> listData = Util.DataTableExtensions.ToList<ec_enginedata_relEntity>(dt);
                        foreach (var da in listData)
                        {
                            //重新设置创建人创建时间，清空更新人更新时间
                            da.CreateUserID = userInfo.userId;
                            da.CreateTime = Time.MySqlTime;
                            da.UpdateTime = null;
                            da.UpdateUserID = null;
                        }
                        //db.InsertByNoMap(listData, $"ec_enginedata_rel_{ ProjectIndex}");
                        _enginedataRelRepository.AsInsertable(listData).AS($"ec_enginedata_rel_{ProjectIndex}").ExecuteCommand();
                        continue;
                    }
                    if (table == "ec_measuring_unit")
                    {
                        //删除原来的表数据
                        SqlSugarHelper.Db.Ado.ExecuteCommand($" DELETE FROM ec_measuring_unit_{ProjectIndex}; ");
                        //获取sqlite数据库里面的数据
                        DataTable dt = SqliteHelper.GetData($" SELECT * FROM ec_measuring_unit ");
                        List<ec_measuring_unitEntity> listData = Util.DataTableExtensions.ToList<ec_measuring_unitEntity>(dt);
                        foreach (var da in listData)
                        {
                            //重新设置创建人创建时间，清空更新人更新时间
                            da.CreateUserID = userInfo.userId;
                            da.CreateTime = Time.MySqlTime;
                            da.UpdateTime = null;
                            da.UpdateUserID = null;
                        }
                        //db.InsertByNoMap(listData, $"ec_measuring_unit_{ ProjectIndex}");
                        _measuringUnitRepository.AsInsertable(listData).AS($"ec_measuring_unit_{ProjectIndex}").ExecuteCommand();
                        continue;
                    }
                    if (table == "ec_measuring_unittype")
                    {
                        //删除原来的表数据
                        SqlSugarHelper.Db.Ado.ExecuteCommand($" DELETE FROM ec_measuring_unittype_{ProjectIndex}; ");
                        //获取sqlite数据库里面的数据
                        DataTable dt = SqliteHelper.GetData($" SELECT * FROM ec_measuring_unittype ");
                        List<ec_measuring_unittypeEntity> listData = Util.DataTableExtensions.ToList<ec_measuring_unittypeEntity>(dt);
                        foreach (var da in listData)
                        {
                            //重新设置创建人创建时间，清空更新人更新时间
                            da.CreateUserID = userInfo.userId;
                            da.CreateTime = Time.MySqlTime;
                            da.UpdateTime = null;
                            da.UpdateUserID = null;
                        }
                        //db.InsertByNoMap(listData, $"ec_measuring_unittype_{ ProjectIndex}");
                        _measuringUnittypeRepository.AsInsertable(listData).AS($"ec_measuring_unittype_{ProjectIndex}").ExecuteCommand();
                        continue;
                    }
                }
                _annexesFileRepository.InsertRange(addAnnexesFile);
                SqlSugarHelper.Db.CommitTran();
            }
            catch (Exception ex)
            {
                SqlSugarHelper.Db.RollbackTran();
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }
        #endregion

    }
}
